<template>
    <view class="" v-if="promotion && showPromotion">
        <!--团购活动-->
        <goods-prom-bar
          v-if="promotion.promotion_type === 'GROUPBUY'"
          title="团购活动"
          type="groupbuy"
          :price="promotion.groupbuy_goods_vo.price"
          :oldprice="promotion.groupbuy_goods_vo.original_price"
          :endtime="promotion.end_time - parseInt(new Date() / 1000)"
          @count-end="handleCountEnd"
        />
        <!--限时抢购-->
        <goods-prom-bar
          v-else
          title="限时抢购"
          type="seckill"
          :price="promotion.seckill_goods_vo.seckill_price"
          :oldprice="promotion.seckill_goods_vo.original_price"
          :endtime="promotion.seckill_goods_vo.distance_end_time"
          @count-end="handleCountEnd"
        />
    </view>
</template>

<script>
    /**
     * 商品详情页团购、限时抢购组件
     *
     * ===== 使用场景 ======
     * 商品详情页。
     *
     * ===== 参数 =====
     * promotions      促销活动
     * selectedSku     商品规格
     */
    import GoodsPromBar from './-goods-prom-bar.vue'
	export default {
        data() {
            return {
                promotion: '',
                showPromotion: true
            }
        },
        props: {
            promotions: {
                type: Array,
                default() {
                    return []
                }
            },
            selectedSku: {
                type: Object,
                default() {
                    return {}
                }
            }
        },
        components: {
            GoodsPromBar
        },
        watch: {
            selectedSku(val) {
                if (!this.promotions || !this.promotions.length) return
                const prom = this.promotions.filter(item => item.groupbuy_goods_vo || item.seckill_goods_vo)
                const _prom = prom.filter(key => key.sku_id === val.sku_id)
                if (_prom && _prom[0]) {
                    this.showPromotion = true
                    if (_prom[0].promotion_type === 'GROUPBUY') {
                        this.promotion = _prom[0]
                    } else {
                        if (_prom[0].seckill_goods_vo.distance_start_time < 0) {
                            this.showPromotion = false
                        } else {
                            this.showPromotion = true
                        }
                        this.promotion = _prom[0]
                    }
                }
                // 如果都没有，返回false
                if (!_prom || !_prom[0]) {
                    this.showPromotion = false
                    return
                }
            }
        },
		methods: {
            handleCountEnd() {
                this.showPromotion = false
                this.$u.toast('活动已结束，商品已恢复原价。')
            }
        }
	}
</script>

<style lang="scss" scoped>

</style>
